/*********************************************************
 *
 * Filename: cm_camera.h
 *   Author: jmdvirus
 *   Create: 2020年03月17日 星期二 12时24分00秒
 *
 *********************************************************/

#ifndef __CM_CAMERA_H
#define __CM_CAMERA_H

#ifdef __cplusplus
extern "C"
{
#endif

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/videodev2.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#define vfe_camera 0
#define vin_camera 1
#define usb_camera 2

#define N_WIN_SIZES 20

#ifdef __USE_VIN_ISP__
#include "AWIspApi.h"
#include "sunxi_camera_v2.h"
#endif

    struct buffer
    {
        void *start[3];
        size_t length[3];
    };

    struct v4l2_frmsize
    {
        unsigned int width;
        unsigned int height;
    };

    struct vfe_format
    {
        unsigned char name[32];
        unsigned int fourcc;
        struct v4l2_frmsize size[N_WIN_SIZES];
    };

    typedef struct camera_hal
    {
        int camera_index;
        int videofd;
        unsigned char isDefault;
        int camera_type;
        int sensor_type;
        int ispId;
#ifdef __USE_VIN_ISP__
        AWIspApi *ispPort;
#endif
        int photo_type;
        int photo_num;
        char save_path[32];
        struct buffer *buffers;
        int buf_count;
        int nplanes;
        unsigned int win_width;
        unsigned int win_height;
        unsigned int pixelformat;
        struct v4l2_buffer buf;
    } camera_handle;

    int cm_camera_init(camera_handle *h, int index, int width, int height);
    int cm_camera_deinit(camera_handle *h);
    int cm_camera_open(camera_handle *h);
    int cm_camera_close(camera_handle *h);
    int cm_camera_start(camera_handle *h);
    int cm_camera_stop(camera_handle *h);
    int cm_camera_stream_get(camera_handle *h, unsigned char *data, int len);
    int cm_camera_stream_get2(camera_handle *h, unsigned char **data, size_t *len);
    // get2 down call it
    int cm_camera_stream_qbuf(camera_handle *h);

#ifdef __cplusplus
}
#endif

#endif
